import time
from PyQt5.QtWidgets import QApplication
import pyautogui


class main_handler():
    label = None
    window = None

    def handler_pic_show_time():
        beginPosX = 486
        beginPosY = 615
        main_handler.window.showMinimized()  # 缩小主窗口
        with open("srt_txt/片段时长.txt", "r") as file:
            for line in file:
                main_handler.click(beginPosX, beginPosY, int(line.strip()))

    def get_mouse_pos():
        for i in range(3, 0, -1):
            QApplication.processEvents()
            time.sleep(1)
            main_handler.label.setText("剩余时间：" + str(i))
        current_x, current_y = pyautogui.position()
        print(current_x, current_y)

    def click(beginPosX, beginPosY, strtime):
        # 将鼠标移动到相对位置（100，100）
        sltime = 0.6
        time.sleep(sltime)
        pyautogui.moveTo(beginPosX, beginPosY)
        # 点击鼠标左键
        pyautogui.click(button='left')
        time.sleep(sltime)
        # 设置时间
        pyautogui.moveTo(954, 443)
        pyautogui.click(button='left')
        time.sleep(sltime)
        pyautogui.press('backspace')
        print(main_handler.millis_to_time(strtime))
        pyautogui.typewrite(main_handler.millis_to_time(strtime))
        time.sleep(sltime)
        # 点击确定
        pyautogui.moveTo(1036, 549)
        pyautogui.click(button='left')
        time.sleep(sltime)
        pyautogui.hotkey('ctrl', 'right')


    def millis_to_time(millis):
        millis = int(millis)
        frames = (millis % 1000) * 24 // 1000
        millis = millis // 1000
        seconds = millis % 60
        millis = millis // 60
        minutes = millis % 60
        millis = millis // 60
        hours = millis

        return f"{seconds:02d}{frames:02d}"
